<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <meta charset="UTF-8">
		<title>所有卫生信息</title>
		<link rel="stylesheet" href="./layui/css/layui.css" />			
		<script type="text/javascript" src="./layui/layui.js" ></script>
	</head>
	<body>
		<div class="layui-container">
			<div class="layui-row">
				<fieldset class="layui-elem-field layui-field-title">
					<legend>所有卫生信息</legend>
				</fieldset>
				<!-- 搜索栏 -->
				<div class="searchTable">
	  				<div class="layui-inline">
	    				<input class="layui-input" name="id" id="dataReload" autocomplete="off" placeholder="请输入宿舍号">
	  				</div>
	  				<button class="layui-btn" data-type="reload" id="selectHealthInfo">搜索</button>
				</div>
				<!-- 展示所有卫生信息表格 -->
				<table class="layui-table" id="allHealth" lay-filter="allHealth"></table>
			</div>
	
		<script type="text/html" id="utilbar">
			<div>
				<button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
				<button class="layui-btn layui-btn-sm" lay-event="del">删除</button>
			</div>
		</script>
		
			<script type="text/javascript">
			layui.use(['table','layer','jquery','form'],function(){
				var table = layui.table;
				var layer = layui.layer;
				var form = layui.form;
				var $ = layui.$;
	//-----------------初始化table
				table.render({
					elem:"#allHealth",
					height: 510,
					url:'selectAllHealthInfoServlet',
					id:'allHealthTable',
					limit:10,
					toolbar:true,
					limits:[5,10,20],
					page:true,
			//-------定义表头
					cols: [[
				      	 {field:'Health_info_number', title:'ID', width:80, fixed: 'left',  sort: true, totalRowText: '合计行'}
				      	,{field:'Dorm_number', title:'宿舍', width:80}
				      	,{field:'Floor_number', title:'楼层', width:80,sort: true}
				      	,{field:'Deduction', title:'扣分明细', width:100}
				      	,{field:'The_Value', title:'所扣分值', width:100,sort: true}
				      	,{field:'Grade', title:'卫生等级', width:100}
				      	,{field:'health_type',title:'卫生类型',width:100}
				      	,{field:'Att_time', title:'时间',  width:110}
				      	,{field:'Remarks', title:'备注',  width:120}
				      	,{field:'',title:'操作', width:200,toolbar:"#utilbar",align:'center'}
					    ]],
			//-----自定义返回值
					    response: {
				      		statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0  
				    	}
					    ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
					      return {
					        "code": res.status, //解析接口状态
					        "msg": res.message, //解析提示文本
					        "count": res.datasize, //解析数据长度
					        "data": res.rows.item //解析数据列表
					      };
					    }
				});
				//数据重载
				var active = {
					reload:function(){
						//获取输入值
						var dataReload = $("#dataReload");
						var index = layer.msg('查询中...请稍后...',{icon:16,time:false,shade:0});
						setTimeout(function(){
							table.reload('allHealthTable',{
									page:{
										curr:1//重新从第一页开始
									},
									where:{
										Dorm_number:dataReload.val()
									}
								});
								layer.close(index);
						},1500);
					}
				};
				//监听btn
				$('#selectHealthInfo').on('click',function(){
						var type = $(this).data('type');
						active[type] ? active[type].call(this) : '';
				});
				//监听操作栏
				table.on('tool(allHealth)',function(obj){
					var data = obj.data;
					//获取用户点击的按钮
					if(obj.event == 'del'){ //删除
						layer.msg('真的删除行么',{
							time:0,//不自动关闭
							btn: ['确定','取消'] ,
							yes : function(index){//确定
								//删除当前行
        						//obj.del();
        						layer.close(index);
        						var healthId =data.Health_info_number;
        						delHealthInfo(healthId,table);
      							}
							}
						);	
					}else if(obj.event == 'update'){ //编辑
						layer.open({
							type:2,
							title:'编辑卫生',
							skin:'layui-layer-rim',
							area: ['90%','65%'],    //宽高
							 anim:5,
        					content:'managerPage/editHealthInfo.jsp',
        					success: function( layero,index){		
        						var body = layer.getChildFrame('body',index);
        						body.contents().find("#hNub").val(data.Health_info_number);
        						body.contents().find("#dNub").val(data.Dorm_number);
        						body.contents().find("#Floor_number").val(data.Floor_number);
        						body.contents().find("#Deduction").val(data.Deduction);
        						body.contents().find("#The_Value").val(data.The_Value);
        						body.contents().find("#health_type").val(data.health_type);
        						body.contents().find("#Grade").val(data.Grade);
        						body.contents().find("#Remarks").val(data.Remarks);
        					},
        					end: function(){
        					//刷新父页面
        					var index = layer.msg('修改成功正在刷新页面...请稍后...',{icon:16,time:false,shade:0});
							setTimeout(function(){
							table.reload('allHealthTable',{
									where:{
										Dorm_number:null
									}
								});
								layer.close(index);
							},500);
        					}
						});
						//渲染表格
						form.render();
					}else if(obj == 'add'){ //添加
						
					}
				});
				
				
				//删除卫生信息
			function delHealthInfo(healthId,table){
				$.ajax({
					type:"post",
					url:"deleteHealthInfoServlet",
					async:true,
					data:{
						healthId:healthId
					},
					success:function(data){
						console.log("update"+data);
						if(data == 1){
							//刷新父页面
        					var index = layer.msg('删除成功正在刷新页面...请稍后...',{icon:16,time:false,shade:0});
							setTimeout(function(){
							table.reload('allHealthTable',{
									where:{
										Dorm_number:null
									}
								});
								layer.close(index);
							},1500);
						}else{
							layer.msg("error");
						}
					},
					error : function(xhr, status, errMsg){
						console.log("status:"+status+"errMsg:"+errMsg);
					}
				});
			};
			});
			
			
		</script>
	</body>
</html>
